package com.zprmb.udzt;

import org.json.JSONObject;

import android.content.Intent;
import android.net.http.SslError;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.SslErrorHandler;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.zprmb.udzt.http.AjaxParams;
import com.zprmb.udzt.util.Constant;
import com.zprmb.udzt.util.VolleryUtil;

public class ExperienceProductDeaily extends BaseActivity implements OnClickListener{
	private TextView tv_rate,tv_time,tv_way;
	private String id;
	private RelativeLayout rl_back;
	private WebView wv;
	private Button btn_of;
	private ProgressBar bar;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.exproduct_deaily);
		initview();
	}
	private void initview() {
		// TODO Auto-generated method stub
		tv_rate=(TextView)findViewById(R.id.tv_rate);
		tv_time=(TextView)findViewById(R.id.tv_time);
		tv_way=(TextView)findViewById(R.id.tv_way);
		wv=(WebView)findViewById(R.id.wv);
		btn_of=(Button)findViewById(R.id.btn_of);
		rl_back=(RelativeLayout)findViewById(R.id.rl_back);
		bar=(ProgressBar)findViewById(R.id.myProgressBar);
		wv.setWebChromeClient(new WebChromeClient(){
			 @Override
	          public void onProgressChanged(WebView view, int newProgress) {
				Log.e("进度", newProgress+""); 
	              if (newProgress == 100) {
	                  bar.setVisibility(View.GONE);
	              } else {
	                  if (View.INVISIBLE == bar.getVisibility()) {
	                      bar.setVisibility(View.VISIBLE);
	                  }
	                  bar.setProgress(newProgress);
	              }
	              super.onProgressChanged(view, newProgress);
	          }
			
		});
		rl_back.setOnClickListener(this);
		btn_of.setOnClickListener(this);
		id=getIntent().getStringExtra("id");
		tv_rate.setText(getIntent().getStringExtra("rate")+"%");
		tv_time.setText(getIntent().getStringExtra("time")+"天");
		tv_way.setText(getIntent().getStringExtra("way"));
		wv.loadUrl(Constant.URL+"/h5/expIntro?app=true");
	
		
		wv.getSettings().setSupportZoom(true);   
		wv.getSettings().setUseWideViewPort(true);    
		wv.getSettings().setLoadWithOverviewMode(true);    
		wv.getSettings().setSavePassword(true);    
		wv.getSettings().setSaveFormData(true);   
		wv.getSettings().setJavaScriptEnabled(true);    
		// enable navigator.geolocation   
		wv.getSettings().setGeolocationEnabled(true);     
		wv.getSettings().setGeolocationDatabasePath("/data/data/org.itri.html5webview/databases/");    
		// enable Web Storage: localStorage, sessionStorage    
		wv.getSettings().setDomStorageEnabled(true); 
		wv.requestFocus(); 
		wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
//		v4_webview.setScrollBarStyle(0);
		String ua = wv.getSettings().getUserAgentString();
		wv.getSettings().setUserAgentString(ua+"; "+"rmbbox_android");
		
		wv.getSettings().setBuiltInZoomControls(true);
		wv.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
		wv.getSettings().setDomStorageEnabled(true);  
		   // Set cache size to 8 mb by default. should be more than enough  
		wv.getSettings().setAppCacheMaxSize(1024*1024*8);  
		   // This next one is crazy. It's the DEFAULT location for your app's cache  
		   // But it didn't work for me without this line.  
		   // UPDATE: no hardcoded path. Thanks to Kevin Hawkins  
		   String appCachePath = getApplicationContext().getCacheDir().getAbsolutePath();  
		   
		   wv.getSettings().setAllowFileAccess(true);  
		   wv.getSettings().setAppCacheEnabled(true);
		   wv.getSettings().setJavaScriptEnabled(true);
		   wv.getSettings().setAppCachePath(appCachePath);
		   wv.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
		   wv.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
		   wv.getSettings().setSupportZoom(true); 
		   wv.getSettings().setDefaultTextEncodingName("utf-8");
		wv.setWebViewClient(new WebViewClient(){
			public boolean shouldOverrideUrlLoading(WebView view, String url){ 
				view.loadUrl(url);
				

				return true;
			}
			public void onReceivedSslError(WebView view,SslErrorHandler handler, SslError error) {
				//super.onReceivedSslError(view, handler, error);
				handler.proceed();
			}
		});
		
	}
	private void invest(){
		AjaxParams params=new AjaxParams();
		params.put("userExpId", id);
		
		VolleryUtil volleryUtil=new VolleryUtil() {
			
			
			
			@Override
			public void onUpDate(JSONObject jsonObject) {
				// TODO Auto-generated method stub
				Log.e("invest", jsonObject.toString());
				if(jsonObject.optInt("success")==1){
					Intent intent1=new Intent(ExperienceProductDeaily.this,MyExperienceGold.class);
					 startActivity(intent1);
					 ExperienceProductDeaily.this.finish();
				}
			}
			
			@Override
			public void onError() {
				// TODO Auto-generated method stub
				
			}
		};
		volleryUtil.setHeaders("token", getToken());
		volleryUtil.setRequestMethod(1);
		volleryUtil.setShow(true);
		volleryUtil.execute(ExperienceProductDeaily.this, Constant.URL+"/api/v5/experience/sureExpInvest?", params.toString());
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch(v.getId()){
		case R.id.btn_of:
			invest();
			break;
		case R.id.rl_back:
			finish();
			break;
		}
	}

}
